package ro.imc.ums.domnica;

import java.beans.XMLDecoder;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.NoSuchElementException;

public class DecodeXML {
	public List<Person> decodeXML(String path){
		List <Person> list = new ArrayList<Person>();
		List <Person> list1 = new ArrayList<Person>();
	
		String files;
		File folder = new File(path);
		File[] listOfFiles = folder.listFiles();
		
		
		for(int i=0;i<listOfFiles.length;i++){
			if(listOfFiles[i].isFile()){
				files = listOfFiles[i].getName();
				if(files.endsWith("xml")){
					System.out.println(listOfFiles[i].getPath());	
		
		XMLDecoder decoder = null;
		try{
			decoder = new XMLDecoder(new BufferedInputStream(new FileInputStream(listOfFiles[i].getPath())));
			list = (List<Person>) decoder.readObject();	
			list1.addAll(list);
		} catch (FileNotFoundException exception){
			exception.printStackTrace();
		} catch (NoSuchElementException exception){
			exception.printStackTrace();
		}
		if(decoder != null){
			decoder.close();
		}
		
				}
			}
			
		}
		return list1;
		
	}						
}
